#!/bin/sh

#----------------------测试1，2环境成功后----------------------
#----------------------开始配置pbft环境----------------------
cd $GOPATH/src/github.com/hyperledger
git clone https://gitee.com/mirrors/hyperledger-fabric.git
mv hyperledger-fabric/ fabric/
cd fabric
git checkout v1.4.4


#4.pbft配置
#将pbft文件夹放到/ fabric/orderer/consensus文件夹下
# common/tools/configtxgen/localconfig/config.go:388
switch ord.OrdererType {
case "pbft":
}

# commom/tools/configtxgen/encoder/encoder.go:38
ConsensusTypePbft = "pbft"

# commom/tools/configtxgen/encoder/encoder.go:215
switch conf.OrdererType {
case ConsensusTypePbft:
}

# orderer/common/server/main.go
#头部 import 57行添加：
"github.com/hyperledger/fabric/orderer/consensus/pbft"
#注释48行：
##"github.com/hyperledger/fabric/orderer/consensus/etcdraft"
#注释713，714行：
raftConsenter := etcdraft.New(clusterDialer, conf, srvConf, srv, registrar, icr, metricsProvider)
consenters["etcdraft"] = raftConsenter
#715行添加
consenters["pbft"] = pbft.New()


#5.编译
make release


#6.编译docker
cd $GOPATH/src/github.com/hyperledger/fabric
git clone https://gitee.com/he_xiaotong/build.git
chmod -R 777 build
mkdir -p .build/docker/gotools
cp -r build/bin/ .build/docker/gotools/bin/

#拉3个文件：Makefile和gotools.mk，images/tools/Dockerfile.in
#7。#改文件
---修改fabric/Makefile,255行开始
下面添加：-v $(abspath build/bin):/opt/gotools/tmp \
修改后:
$(BUILD_DIR)/docker/gotools: gotools.mk
    @echo "Building dockerized gotools"
    @mkdir -p $@/bin $@/obj
    @$(DRUN) \
        -v $(abspath $@):/opt/gotools \
        -v $(abspath build/bin):/opt/gotools/tmp \
        -w /opt/gopath/src/$(PKGNAME) \
        $(BASE_DOCKER_NS)/fabric-baseimage:$(BASE_DOCKER_TAG) \
        make -f gotools.mk GOTOOLS_BINDIR=/opt/gotools/bin GOTOOLS_GOPATH=/opt/gotools/obj
---修改fabric/Makefile注释226行:
curl -fL $(CHAINTOOL_URL) > $@
改成：
cp build/chaintool $@
---修改fabric/gotools.mk文件,注释掉第22行，并将其修改成如下：
## 修改之前的
.PHONY: gotools-install
gotools-install: $(patsubst %,$(GOTOOLS_BINDIR)/%, $(GOTOOLS))
## 修改之后的
.PHONY: gotools-install
gotools-install:
    cp /opt/gotools/tmp/* $(GOTOOLS_BINDIR)
---fabric/images/tools/Dockerfile.in：14

#第14行下面加一句
ENV DEBIAN_FRONTEND noninteractive
#拉相关手动编译的包
make peer-docker
make orderer-docker
make ccenv
make buildenv
make tools-docker
docker rmi $(docker images | grep "none" | awk '{print $3}')



#----------------------pbft网络测试----------------------
#1.将pbft-network文件夹复制到 $GOPATH/src/github.com/hyperledger/下
#修改：
pbft-network/base/peer-base.yaml:4和17行
pbft-network/docker-compose-cli.yaml59行

#2.复制bin到pbft-network,让bin生效
cp -r $GOPATH/src/github.com/hyperledger/fabric/release/linux-amd64/bin $GOPATH/src/github.com/hyperledger/pbft-network/bin
export PATH=$PATH:$GOPATH/src/github.com/hyperledger/pbft-network/bin
peer version
orderer version
cryptogen version
configtxgen -version
configtxlator version


#3.创建证书和通道配置文件
cryptogen generate --config ./crypto-config.yaml
mkdir channel-artifacts
configtxgen --profile Genesis -outputBlock ./channel-artifacts/genesis.block
configtxgen --profile Channel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID mychannel

#4.启动网络
docker-compose -f docker-compose-cli.yaml up -d
docker ps -a

#5.停止和移除镜像：
docker-compose -f docker-compose-cli.yaml down
docker volume prune
docker ps -a


